<template>
  <!-- 民警管理 -- 新增/编辑 -->
  <div>
    <public-modal
      width="500px"
      :footer="false"
      :title="title"
      :publicVisible="publicVisible"
      @handleOk="handleOk"
      @cancel="cancel"
    >
      <div slot="content">
        <a-form-model
          ref="form"
          :model="form"
          :rules="rules"
          class="formStyle"
          autocomplete="off"
          layout="inline"
          :labelCol="{ style: 'width: 100px' }"
        >
          <a-row :gutter="24">
            <a-col :span="24">
              <a-form-model-item label="设备IMEI码:" prop="imei">
                <a-input :maxLength="30" autocomplete="off" placeholder="请输入设备IMEI码" v-model="form.imei" />
              </a-form-model-item>
              <a-form-model-item label="SIM卡号码:" prop="simNumber">
                <a-input :maxLength="30" autocomplete="off" placeholder="请输入SIM卡号码" v-model="form.simNumber" />
              </a-form-model-item>
              <a-form-model-item label="所属医疗机构:">
                <a-input
                  :maxLength="30"
                  autocomplete="off"
                  placeholder="请输入所属医疗机构"
                  v-model="form.departmentName"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </public-modal>
  </div>
</template>

<script>
import { uploadFile } from '@/api/api'
import { checkIDCard } from '@/utils/validate.js'
import publicModal from '@/components/publicModal'
import { putAction, postAction } from '@/api/manage'
import { isMobile } from '@/utils/validate'
import { pick } from 'lodash-es'

export default {
  name: 'addPoliceModal',
  components: { publicModal },
  props: {
    idCode: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      title: '新增',
      record: {},
      publicVisible: false,
      imageUrl: [],
      fileList: [],
      showUploadList: { showPreviewIcon: false },
      form: {},
      rules: {
        imei: [{ required: true, message: '请输入设备IMEI码!' }],
      },
      actionType: 'add',
    }
  },
  mounted() {},
  methods: {
    async show(record = {}, actionType = 'add') {
      this.record = record
      this.publicVisible = true
      this.actionType = actionType
      if (record && record.id) {
        this.title = `编辑设备`
        this.form = pick(record, ['id', 'imei', 'simNumber', 'departmentName'])
      } else {
        this.title = `新增设备`
      }
    },

    remove(file) {
      let uid = file.uid
      let index = this.fileList.findIndex((v) => {
        return v.uid == uid
      })
      this.fileList.splice(index, 1)
      this.imageUrl.splice(index, 1)
    },
    async customRequest(info) {
      let file = info.file
      const formData = new FormData()
      formData.append('file', file)
      if (this.status == 0) formData.append('type', 2)
      else if (this.status == 1) formData.append('type', 63)
      else if (this.status == 2) formData.append('type', 64)
      let res = await uploadFile(formData)
      if (res.code == 200) {
        this.imageUrl.push(res.result.halfPath)
      }
    },
    uploadChange({ file, fileList }) {
      file.status = 'done'
      this.fileList = fileList
    },

    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return

        let url = ''
        let parmas = {}
        if (this.actionType == 'add') {
          url = '/wb/device/addDevice'
          parmas = { policeAgencyId: this.idCode, ...this.form }
        } else {
          url = '/wb/device/updateDeviceInfo'
          parmas = {
            id: this.record.id,
            ...this.form,
          }
        }
        const method = this.record.id ? postAction : postAction
        try {
          const res = await method(url, parmas)
          this.$message.success(res.message)
          this.$emit('refresh')
          this.cancel()
        } catch (error) {
          // 处理错误
          console.error('请求失败:', error)
          this.$message.error('请求失败，请稍后重试！')
        }
      })
    },

    cancel() {
      this.publicVisible = false
      this.fileList = []
      this.imageUrl = []
      this.form = {}
    },
  },
}
</script>

<style scoped lang="less">
.formStyle {
  padding: 0.2rem;
  /deep/.ant-col {
    height: unset;
  }
  .ant-form-item-with-help {
    margin-bottom: 0px !important;
  }
  .ant-form-item {
    margin-bottom: 20px !important;
  }
  /deep/ .ant-form-explain {
    position: absolute;
  }
}

.imageUpload {
  margin-bottom: 150px;
  /deep/.avatar-uploader {
    > .ant-upload {
      width: 150px;
      height: 176px;
      background: var(--theme-input-bg);
      border: 1px solid var(--theme-input-border);
    }
  }

  /deep/.ant-upload-select-picture-card {
    width: 150px;
    height: 176px;
  }

  /deep/ .ant-upload-list-item {
    width: 150px;
    height: 176px;
  }

  .imgText {
    color: #1163b8;
    font-weight: 500;
  }

  .pictIcon {
    margin-bottom: 10px;
    width: 42px;
    height: 38px;
  }
}
</style>
